<?php 
session_start();
class chart extends CI_Controller  {

        /**
        * Index Page for this controller.
        *
        * Maps to the following URL
        * 		http://example.com/index.php/welcome
        *	- or -  
        * 		http://example.com/index.php/welcome/index
        *	- or -
        * Since this controller is set as the default controller in 
        * config/routes.php, it's displayed at http://example.com/
        *
        * So any other public methods not prefixed with an underscore will
        * map to /index.php/welcome/<method_name>
        * @see http://codeigniter.com/user_guide/general/urls.html
        */
       private $_configs = null;
       function __construct()
        {
            parent::__construct();
            $this->load->helper('url');
            $this->load->library('javascript');
            $this->load->library('session');
            $this->load->library('smarty3','','smarty');
            $this->_configs["title"]="Báo Cáo - Biểu Đồ";
            $this->_configs["authority"]="chart";
            $this->_configs["MinAuthority"]=0;
            $this->load->model('admin-planners/report_model','report_model');
            if($this->checkauthority()<0){AccessDenied();}
            
        }
        public function index()
	{
            $this->smarty->assign('_SESSION', $_SESSION);
            $this->smarty->assign('configs', $this->_configs);
            $this->smarty->view('admin-planners/chart/00_chart','contentofview');
            $this->smarty->view('admin-planners/00_1_styles','styles');
            $this->smarty->view('admin-planners/00_2_scripts','scripts');
            $this->smarty->view('admin-planners/00_3_menus','menus');
            $this->smarty->view('admin-planners/10_template','view');
            $this->smarty->display('admin-planners/01_manage');
	}
        function display(){
            
        }
        function month(){
            $this->smarty->display('admin-planners/chart/03_yeari');
        }
        function Chart_Spline_Series(){
            $this->smarty->display('admin-planners/chart/04_Chart_Spline_Series');
            
        }
        function Line_Series_Month($month=null,$year=null){
            if($month==null)$month=date("m");
            if($year==null)$year=date("Y");
            $mindate = date("m-d-Y",mktime(0, 0, 0, $month, 1,   $year));
            $maxdate = date("m-d-Y",mktime(0, 0, 0, $month+1, 0,   $year));
            $configs["title"]="Biểu đồ doanh thu tháng $month năm $year";
            $configs["month"]=$month;
            $configs["year"]=$year;
            $configs["mindate"]=$mindate;
            $configs["maxdate"]=$maxdate;
            
            $startdate = date("Y-m-d",mktime(0, 0, 0, $month, 1,   $year));
            $enddate = date("Y-m-d",mktime(0, 0, 0, $month+1, 0,   $year));
            $data=$this->report_model->Line_Series_Data_Month($startdate,$enddate);
            $maxCash=1000000;
            if(count($data)>0){
                foreach ($data as $r){

                    if($maxCash<$r->TotalCash){
                        $maxCash=$r->TotalCash;
                    }
                }
            }
            
            $maxCash=  round($maxCash,6);
            $configs["maxCash"]=$maxCash;
            if($maxCash>100000000){$configs["unitInterval"]=20000000;$maxCash=  round($maxCash,7);}
            elseif($maxCash>50000000){$configs["unitInterval"]=10000000;$maxCash=  round($maxCash,7);}
            elseif($maxCash>10000000){$configs["unitInterval"]=2000000;}
            elseif($maxCash>5000000){$configs["unitInterval"]=1000000;}
            else {$configs["unitInterval"]=500000;}
            $this->smarty->assign('configs', $configs);
            $this->smarty->display('admin-planners/chart/02_Line_Series');
        }
        
        function Chart_Gray_Scale_Month($month=null,$year=null){
            if($month==null)$month=date("m");
            if($year==null)$year=date("Y");
            $mindate = date("m-d-Y",mktime(0, 0, 0, $month, 1,   $year));
            $maxdate = date("m-d-Y",mktime(0, 0, 0, $month+1, 0,   $year));
            $configs["title"]="Biểu đồ doanh thu tháng $month năm $year";
            $configs["month"]=$month;
            $configs["year"]=$year;
            $configs["mindate"]=$mindate;
            $configs["maxdate"]=$maxdate;
            
            $startdate = date("Y-m-d",mktime(0, 0, 0, $month, 1,   $year));
            $enddate = date("Y-m-d",mktime(0, 0, 0, $month+1, 0,   $year));
            $data=$this->report_model->Chart_Gray_Scale_Month($startdate,$enddate);
            
            $maxCash=1000000;
            $minCash=1000000;
            $total=0;
            if(count($data)>0){
                foreach ($data as $r){
                    $total+=round($r->TotalCash,0)+round($r->TotalCashUnFinish,0);
                    if($minCash>$r->TotalCash){
                        $minCash=$r->TotalCash;
                    }
                    if($minCash>$r->TotalCashUnFinish){
                        $minCash=$r->TotalCashUnFinish;
                    }
                    if($maxCash<($r->TotalCash+$r->TotalCashUnFinish)){
                        $maxCash=$r->TotalCash+$r->TotalCashUnFinish;
                    }
                    $data[date("m-d-Y", strtotime($r->OrderDate))]=array(
                        "OrderDate"=>date("m-d-Y", strtotime($r->OrderDate)),
                        "TotalCash"=>  round($r->TotalCash,0),
                        "TotalCashUnFinish"=>  round($r->TotalCashUnFinish,0),
                        "TotalCashAll"=>round($r->TotalCash,0)+round($r->TotalCashUnFinish,0)
                        );
                }
                $numdate = date("d",mktime(0, 0, 0, 10+1, 0,   2012));
                for($d=1;$d<=$numdate;$d++){
                    $dd=$d<10?"0$d":$d;
                    if(isset($data["$month-$dd-$year"])){
                        $result[]=$data["$month-$dd-$year"];
                    }else{
                        $result[]=array(
                            "OrderDate"=>"$month-$dd-$year",
                            "TotalCash"=>  0,
                            "TotalCashUnFinish"=>0,
                            "TotalCashAll"=>0
                            );
                    }
                }
            }else{
                $result[]=array("OrderDate"=>$mindate);    
                $result[]=array("OrderDate"=>$maxdate);    
            }
            
            $maxCash=  round($maxCash,6);
            $minCash=$minCash-1000000;
            if($minCash<0)$minCash=0;
            $minCash=  round($minCash,6);
            
            
            if($maxCash>100000000){$configs["unitInterval"]=20000000;$maxCash=  round($maxCash,7);}
            elseif($maxCash>50000000){$configs["unitInterval"]=10000000;$maxCash=  round($maxCash,7);}
            elseif($maxCash>10000000){$configs["unitInterval"]=2000000;}
            elseif($maxCash>5000000){$configs["unitInterval"]=1000000;}
            else {$configs["unitInterval"]=500000;}
            
            $configs["maxCash"]=$maxCash;
            $configs["minCash"]=$minCash;
            $configs["data"]=  json_encode($result);
            $configs["total"]=  number_format($total);
            $this->smarty->assign('configs', $configs);
            //echo "<pre>";print_r($configs); echo "</pre>";
            $this->smarty->display('admin-planners/chart/08_Chart_Gray_Scale');
            
        }
        function Chart_Gray_Scale_Year($year=null){
            if($year==null)$year=date("Y");
            $configs["title"]="Biểu đồ doanh thu t năm $year";
            $configs["year"]=$year;
            $startdate = date("Y-m-d",mktime(0, 0, 0, 1, 1,   $year));
            $enddate = date("Y-m-d",mktime(0, 0, 0, 13, 0,   $year));
            $data=$this->report_model->Chart_Gray_Scale_Year($startdate,$enddate);
            
            $maxCash=1;
            $minCash=1;
            $total=0;
            if(count($data)>0){
                foreach ($data as $r){
                    $t1=round($r->TotalCash,6)/1000000;
                    $t2=round($r->TotalCashUnFinish,6)/1000000;
                    $total+=$t1+$t2;
                    if($minCash>$t1){
                        $minCash=$t1;
                    }
                    if($minCash>$t2){
                        $minCash=$t2;
                    }
                    if($maxCash<$t1+$t2){
                        $maxCash=$t1+$t2;
                    }
                    $data[$r->Month]=array(
                        "Month"=>"Tháng ".$r->Month,
                        "TotalCash"=>  $t1,
                        "TotalCashUnFinish"=>  $t2,
                        "TotalCashAll"=>$t1+$t2
                        );
                }
                for($m=1;$m<=12;$m++){
                    //$mm=$m<10?"0$d":$d;
                    if(isset($data[$m])){
                        $result[]=$data[$m];
                    }else{
                        $result[]=array(
                            "OrderDate"=>"Tháng $m",
                            "TotalCash"=>  0,
                            "TotalCashUnFinish"=>  0,
                            "TotalCashAll"=>0
                            );
                    }
                }
            }else{
                $result[]=array("Month"=>1);    
                $result[]=array("Month"=>12);    
            }
            $minCash=$minCash;
            if($maxCash>200)$configs["unitInterval"]=40;
            elseif($maxCash>100)$configs["unitInterval"]=20;
            elseif($maxCash>50)$configs["unitInterval"]=10;
            elseif($maxCash>20)$configs["unitInterval"]=5;
            else{$configs["unitInterval"]=5;}
            
            $configs["maxCash"]=$maxCash;
            $configs["minCash"]=$minCash;
            $configs["data"]=  json_encode($result);
            $configs["total"]=  number_format($total);
            $this->smarty->assign('configs', $configs);
            //echo "<pre>";print_r($configs); echo "</pre>";
            $this->smarty->display('admin-planners/chart/09_Chart_Gray_Scale_Year');
            
        }
        
        function Line_Series_Symbol_Year($year=null){
            if($year==null)$year=date("Y");
            $mindate = date("m-d-Y",mktime(0, 0, 0, 1, 1,   $year));
            $maxdate = date("m-d-Y",mktime(0, 0, 0, 13, 0,   $year));
            $configs["title"]="Biểu đồ doanh thu  năm $year";
            $configs["year"]=$year;
            $configs["mindate"]=$mindate;
            $configs["maxdate"]=$maxdate;
            
            $startdate = date("Y-m-d",mktime(0, 0, 0, 1, 1,   $year));
            $enddate = date("Y-m-d",mktime(0, 0, 0, 13, 0,   $year));
            $data=$this->report_model->Line_Series_Symbol_Data_Year($startdate,$enddate);
            $maxCash=1000000;
            $minCash=1000000;
            if(count($data)>0){
                $minCash=$data[0]->TotalCash;
                foreach ($data as $r){
                    if($minCash>$r->TotalCash){
                        $minCash=$r->TotalCash;
                    }
                    if($maxCash<$r->TotalCash){
                        $maxCash=$r->TotalCash;
                    }
                }
            }else{
                
            }
            
            $maxCash=  round($maxCash,6);
            $minCash=$minCash-1000000;
            if($minCash<0)$minCash=0;
            $minCash=  round($minCash,6);
            $configs["maxCash"]=$maxCash;
            $configs["minCash"]=$minCash;
            
            if($maxCash>100000000){$configs["unitInterval"]=20000000;$maxCash=  round($maxCash,7);}
            elseif($maxCash>50000000){$configs["unitInterval"]=10000000;$maxCash=  round($maxCash,7);}
            elseif($maxCash>10000000){$configs["unitInterval"]=2000000;}
            elseif($maxCash>5000000){$configs["unitInterval"]=1000000;}
            else {$configs["unitInterval"]=500000;}
            $this->smarty->assign('configs', $configs);
            $this->smarty->display('admin-planners/chart/06_Line_Series_Symbol');
        }
        function Stacked_Line_Series(){
            $this->smarty->display('admin-planners/chart/05_Stacked_Line_Series');
        }
        public function yeardata(){
            for ($i=1;$i<=31;$i++) {
                    $customers[] = array(
                            'Date'  => "1/$i/2012",
                            'Referral'     => rand(1000, 1500),
                            'SearchPaid'     => rand(1000, 1500),
                            'SearchNonPaid'     => rand(1000, 1500)
                    );
                }
                echo json_encode($customers);
        }
        public function Line_Series_Data_Month($month=null,$year=null){
            if($month==null)$month=date("m");
            if($year==null)$year=date("Y");
            $startdate = date("Y-m-d",mktime(0, 0, 0, $month, 1,   $year));
            $enddate = date("Y-m-d",mktime(0, 0, 0, $month+1, 0,   $year));
            $data=$this->report_model->Line_Series_Data_Month($startdate,$enddate);
            if(count($data)>0){
                foreach ($data as $r){

                    $data[date("m-d-Y", strtotime($r->OrderDate))]=array("OrderDate"=>date("m-d-Y", strtotime($r->OrderDate)),"TotalCash"=>  round($r->TotalCash,0));
                }
                $numdate = date("d",mktime(0, 0, 0, 10+1, 0,   2012));
                for($d=1;$d<=$numdate;$d++){
                    $dd=$d<10?"0$d":$d;
                    if(isset($data["$month-$dd-$year"])){
                        $result[]=$data["$month-$dd-$year"];
                    }else{
                        $result[]=array("OrderDate"=>"$month-$dd-$year","TotalCash"=>  0);
                    }
                }
                
            }else{
                $minday = date("m-d-Y",mktime(0, 0, 0, $month, 1,   $year));
                $result[]=array("OrderDate"=>"$minday","TotalCash"=>0);    
                //$result[]=array("OrderDate"=>"$enddate","TotalCash"=>0);    
            }
            echo json_encode($result);
        }
        public function Line_Series_Symbol_Data_Year($year=null){
            if($year==null)$year=date("Y");
            $startdate = date("Y-m-d",mktime(0, 0, 0, 1, 1,   $year));
            $enddate = date("Y-m-d",mktime(0, 0, 0, 13, 0,   $year));
            $data=$this->report_model->Line_Series_Symbol_Data_Year($startdate,$enddate);
            if(count($data)>0){
                foreach ($data as $r){

                    $data[$r->Month]=array("Month"=>"Tháng ".$r->Month,"TotalCash"=>  round($r->TotalCash,0));
                }
                for($m=1;$m<=12;$m++){
                    //$mm=$m<10?"0$d":$d;
                    if(isset($data[$m])){
                        $result[]=$data[$m];
                    }else{
                        $result[]=array("OrderDate"=>"Tháng $m","TotalCash"=>  0);
                    }
                }
            }else{
                 $result=array();
            }
            echo json_encode($result);
        }
        public function Chart_Donut_Year($year=null){
            if($year==null)$year=date("Y");
            $data=$this->report_model->Chart_Donut_Year($year);
            $total=0;
            if(count($data)>0){
                foreach ($data as $r){
                    $total+=$r->TotalCash;
                    $result[]=array("Quarter"=>"Quí $r->Quater ($".number_format($r->TotalCash).")");
                }
                for($i=0;$i<count($result);$i++){
                    $result[$i]["Pecent"]=round(($data[$i]->TotalCash)/$total*100,2);
                }
                $configs["source"]=json_encode($result);
            }else{
                $result=array();
            }
            $configs["year"]=$year;
            $configs["total"]=$total;
            $this->smarty->assign('configs', $configs);
            $this->smarty->display('admin-planners/chart/07_Chart_Donut');
            //echo json_encode($result);
        }
        public function Chart_Donut_Data_Year($year=null){
            if($year==null)$year=date("Y");
            $data=$this->report_model->Chart_Donut_Year($year);
            $total=0;
            if(count($data)>0){
                foreach ($data as $r){
                    $total+=$r->TotalCash;
                    $result[]=array("Quarter"=>$r->Quater);
                }
                for($i=0;$i<count($result);$i++){
                    $result[$i]["Pecent"]=round(($data[$i]->TotalCash)/$total*100,2);
                }
            }else{
                $result=array();
            }
            echo json_encode($result);
        }
        public function checkauthority(){
            if(isset($_SESSION["ADP-USER"]["AUTHORITY"])){
                $myau=-1;
                if(is_array($_SESSION["ADP-USER"]["AUTHORITY"])){
                    foreach ($_SESSION["ADP-USER"]["AUTHORITY"] as $au){
                        if($au["keyword"]=="view" && $myau==-1)$myau=0;
                        if($au["keyword"]=="admin")$myau=1;
                        if($au["keyword"]==$this->_configs["authority"]){
                            if($au["value"]==0 && $myau==-1) $myau=0;
                            if($au["value"]==1) $myau=1;
                        }
                    }
                }
                return $myau>=$this->_configs["MinAuthority"]?$myau:-1;
            }return -1;
        }
        
        function jqgrid_data(){
            $customers=array();
            $result['total_rows']=0;
            if($this->checkauthority()>=0){
                $result=$this->customer_model->gettotal_rows();
                $rows=$result['rows'];
                // get data and store in a json array
                foreach ($rows as $row) {
                    $customers[] = array(
                            '_CustomerID_'     => $row->CustomerID,
                            '_CustomerName_'     => $row->CustomerName,
                            '_Mail_'=> $row->Mail,
                            '_CompanyName_'=> $row->CompanyName,
                            '_ContactTitle_'=> $row->ContactTitle,
                            '_Address_'=> $row->Address,
                            '_Birth_'  => $row->Birth,
                            '_Phone_'=> $row->Phone,
                            '_Fax_'=> $row->Fax,
                            '_City_'=> $row->City,
                            '_Region_'=> $row->Region,
                            '_Country_'=> $row->Country,
                            '_Type_'      => $row->Type,
                            '_Status_'   => $row->Status,
                            '_Insert_'   => $row->Insert,
                            '_Update_'   => $row->Update,
                            '_Customer_'   => json_encode(array(
                                            "_ID_"=>$row->ID,
                                            "_Delete_"=>$row->Delete,
                                            "_Status_"=>$row->Status
                                        ))
                    );
                }
            }
            $data[] = array(
                'TotalRows' => $result['total_rows'],
                'Rows' => $customers
            );
            echo json_encode($data);
        }
        
        
        
       
        
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */